package org.jeecg.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.system.entity.DataTime;
import org.jeecg.modules.system.entity.ZkClassMessage;
import org.jeecg.modules.system.entity.ZkData1;
import org.jeecg.modules.system.service.IZkClassMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 班次信息表
 * @Author: jeecg-boot
 * @Date:   2024-06-03
 * @Version: V1.0
 */
@Api(tags="班次信息表")
@RestController
@RequestMapping("/zkClassMessage/zkClassMessage")
@Slf4j
public class ZkClassMessageController extends JeecgController<ZkClassMessage, IZkClassMessageService> {
	@Autowired
	private IZkClassMessageService zkClassMessageService;
	
	/**
	 * 分页列表查询
	 *
	 * @param zkClassMessage
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "班次信息表-分页列表查询")
	@ApiOperation(value="班次信息表-分页列表查询", notes="班次信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ZkClassMessage>> queryPageList(ZkClassMessage zkClassMessage,
													   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													   HttpServletRequest req) {
		Result<IPage<ZkClassMessage>> result = new Result<IPage<ZkClassMessage>>();
		Page<ZkClassMessage> page = new Page<ZkClassMessage>(pageNo, pageSize);
		//获取当前登录人信息
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String sysOrgCode=sysUser.getOrgCode();
		zkClassMessage.setSysOrgCode(sysOrgCode);

		IPage<ZkClassMessage> pageList = zkClassMessageService.queryZkClassMessage(page, zkClassMessage);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	
	/**
	 *   添加
	 *
	 * @param zkClassMessage
	 * @return
	 */
	@AutoLog(value = "班次信息表-添加")
	@ApiOperation(value="班次信息表-添加", notes="班次信息表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ZkClassMessage zkClassMessage) {
            zkClassMessageService.save(zkClassMessage);
            return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param zkClassMessage
	 * @return
	 */
	@AutoLog(value = "班次信息表-编辑")
	@ApiOperation(value="班次信息表-编辑", notes="班次信息表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ZkClassMessage zkClassMessage) {
            zkClassMessageService.updateById(zkClassMessage);
            return Result.OK("编辑成功!");

	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "班次信息表-通过id删除")
	@ApiOperation(value="班次信息表-通过id删除", notes="班次信息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		zkClassMessageService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "班次信息表-批量删除")
	@ApiOperation(value="班次信息表-批量删除", notes="班次信息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.zkClassMessageService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "班次信息表-通过id查询")
	@ApiOperation(value="班次信息表-通过id查询", notes="班次信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ZkClassMessage> queryById(@RequestParam(name="id",required=true) String id) {
		ZkClassMessage zkClassMessage = zkClassMessageService.getById(id);
		if(zkClassMessage==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(zkClassMessage);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param zkClassMessage
    */
    @RequiresPermissions("zkClassMessage:zk_class_message:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZkClassMessage zkClassMessage) {
        return super.exportXls(request, zkClassMessage, ZkClassMessage.class, "班次信息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("zkClassMessage:zk_class_message:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZkClassMessage.class);
    }

     /**
      * 班次信息查询（BS端接口）
      *
      * @return
      */
	 @IgnoreAuth
	 @ApiOperation(value="班次信息查询（唯一）", notes="班次信息查询（唯一）")
     @GetMapping(value = "/queryClassMessage")
     public Result<ZkClassMessage> queryClassMessage(String productFloor) {

			 //获取当前时间
			 Date date = new Date();
			 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 String time = sdf.format(date);

            ZkClassMessage zkClassMessage=this.zkClassMessageService.queryZkClassMessageBS(productFloor,time);
            return Result.OK(zkClassMessage);

     }

	 /**
	  * 查询产品名称接口（PC端）
	  *
	  * @return
	  */
	 @ApiOperation(value="查询产品名称接口", notes="查询产品名称接口")
	 @GetMapping(value = "/queryProductSpecsName")
	 public Result<?> queryProductSpecsName(String productFloor) {

	 	List<String> list=this.zkClassMessageService.queryProductSpecsName(productFloor);
		 return Result.OK(list);
	 }

	/**
	 * 根据产品名称查询开始时间和结束时间（PC端）
	 *
	 * @return
	 */
	@ApiOperation(value="根据产品名称查询开始时间和结束时间", notes="根据产品名称查询开始时间和结束时间")
	@PostMapping(value = "/queryDataTime")
	public Result<?> queryDataTime(@RequestBody ZkData1 zkData1) {

		List<DataTime> list=this.zkClassMessageService.queryDataTime(zkData1.getProductSpecsName(),zkData1.getProductFloor());
		return Result.OK(list);
	}

}
